<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>大模型开发实战 | SanShui的个人博客</title><meta name="author" content="SanShui"><meta name="copyright" content="SanShui"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="本文将以百度的文心一言，阿里巴巴的通义千问，科大讯飞的讯飞星火认知大模型，智谱清言的ChartGLM，腾讯的混元大模型，OpenAI的ChatGPT，这六个模型的api调用进行实战代码讲解。">
<meta property="og:type" content="article">
<meta property="og:title" content="大模型开发实战">
<meta property="og:url" content="https://huaiyuechusan.github.io/archives/763f7a6d.html">
<meta property="og:site_name" content="SanShui的个人博客">
<meta property="og:description" content="本文将以百度的文心一言，阿里巴巴的通义千问，科大讯飞的讯飞星火认知大模型，智谱清言的ChartGLM，腾讯的混元大模型，OpenAI的ChatGPT，这六个模型的api调用进行实战代码讲解。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://wallpaper.csun.site/?12">
<meta property="article:published_time" content="2024-06-19T15:01:00.000Z">
<meta property="article:modified_time" content="2024-10-17T11:59:24.695Z">
<meta property="article:author" content="SanShui">
<meta property="article:tag" content="大模型">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://wallpaper.csun.site/?12"><link rel="shortcut icon" href="/./img/config_img/%E9%98%B3%E5%85%89%E5%B0%8F%E7%8C%AB.jpg"><link rel="canonical" href="https://huaiyuechusan.github.io/archives/763f7a6d"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '大模型开发实战',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-10-17 19:59:24'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/echarts@4.9.0/dist/echarts.min.js"></script><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-double-row-display@1.00/cardlistpost.min.css"/>
<style>#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags:before {content:"\A";
  white-space: pre;}#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags > .article-meta__separator{display:none}</style>
<link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-categories-card@1.0.0/lib/categorybar.css"><link rel="stylesheet" href="/./css/runtime.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="/atom.xml" title="SanShui的个人博客" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (true) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/./img/config_img/%E9%98%B3%E5%85%89%E5%B0%8F%E7%8C%AB.jpg" onerror="onerror=null;src='./img/config_img/蓝天.jpg'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">25</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">16</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/comments/"><i class="fa-fw fas fa-envelope-open"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://wallpaper.csun.site/?12')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">SanShui的个人博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/comments/"><i class="fa-fw fas fa-envelope-open"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">大模型开发实战</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-06-19T15:01:00.000Z" title="发表于 2024-06-19 23:01:00">2024-06-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-10-17T11:59:24.695Z" title="更新于 2024-10-17 19:59:24">2024-10-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/AI%E5%A4%A7%E6%A8%A1%E5%9E%8B/">AI大模型</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>14分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="大模型开发实战"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><meta name="referrer" content="no-referrer" /></p>
<h1 id="大模型开发实战"><a href="#大模型开发实战" class="headerlink" title="大模型开发实战"></a>大模型开发实战</h1><p>本文将以百度的文心一言，阿里巴巴的通义千问，科大讯飞的讯飞星火认知大模型，智谱清言的ChartGLM，腾讯的混元大模型，OpenAI的ChatGPT，这六个模型的api调用进行实战代码讲解。</p>
<h2 id="大模型向量数据库创建"><a href="#大模型向量数据库创建" class="headerlink" title="大模型向量数据库创建"></a>大模型向量数据库创建</h2><h3 id="对上传的不同格式文件进行处理"><a href="#对上传的不同格式文件进行处理" class="headerlink" title="对上传的不同格式文件进行处理"></a>对上传的不同格式文件进行处理</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> embedding.call_embedding <span class="keyword">import</span> get_embedding</span><br><span class="line"><span class="keyword">from</span> langchain.document_loaders <span class="keyword">import</span> UnstructuredFileLoader, UnstructuredCSVLoader, CSVLoader</span><br><span class="line"><span class="keyword">from</span> langchain.document_loaders <span class="keyword">import</span> UnstructuredMarkdownLoader</span><br><span class="line"><span class="keyword">from</span> langchain.text_splitter <span class="keyword">import</span> RecursiveCharacterTextSplitter</span><br><span class="line"><span class="keyword">from</span> langchain.document_loaders <span class="keyword">import</span> PyMuPDFLoader</span><br><span class="line"><span class="keyword">from</span> langchain.vectorstores <span class="keyword">import</span> Chroma</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">file_loader</span>(<span class="params">file, loaders</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.isfile(file):</span><br><span class="line">        [file_loader(os.path.join(file, f), loaders) <span class="keyword">for</span> f <span class="keyword">in</span> os.listdir(file)]</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    file_type = file.split(<span class="string">&#x27;.&#x27;</span>)[-<span class="number">1</span>]</span><br><span class="line">    <span class="keyword">if</span> file_type == <span class="string">&#x27;pdf&#x27;</span>:</span><br><span class="line">        loaders.append(PyMuPDFLoader(file))</span><br><span class="line">    <span class="keyword">elif</span> file_type == <span class="string">&#x27;md&#x27;</span>:</span><br><span class="line">        loaders.append(UnstructuredMarkdownLoader(file))</span><br><span class="line">    <span class="keyword">elif</span> file_type == <span class="string">&#x27;txt&#x27;</span>:</span><br><span class="line">        loaders.append(UnstructuredFileLoader(file))</span><br><span class="line">    <span class="keyword">elif</span> file_type == <span class="string">&#x27;csv&#x27;</span>:</span><br><span class="line">        loaders.append(CSVLoader(file))</span><br><span class="line">    <span class="keyword">return</span></span><br></pre></td></tr></table></figure>
<h3 id="切分文档"><a href="#切分文档" class="headerlink" title="切分文档"></a>切分文档</h3><p>:warning:小技巧</p>
<p>在每段分割的文档前后加上文档名，这样可以保证查询的信息是指定文档内的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">create_db</span>(<span class="params">files=DEFAULT_DB_PATH, persist_directory=DEFAULT_PERSIST_PATH, embeddings=<span class="string">&quot;openai&quot;</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    该函数用于加载 PDF 文件，切分文档，生成文档的嵌入向量，创建向量数据库。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    参数:</span></span><br><span class="line"><span class="string">    file: 存放文件的路径。</span></span><br><span class="line"><span class="string">    embeddings: 用于生产 Embedding 的模型</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    返回:</span></span><br><span class="line"><span class="string">    vectordb: 创建的数据库。</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">if</span> files == <span class="literal">None</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;can&#x27;t load empty file&quot;</span></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">type</span>(files) != <span class="built_in">list</span>:</span><br><span class="line">        files = [files]</span><br><span class="line">    loaders = []</span><br><span class="line">    [file_loader(file, loaders) <span class="keyword">for</span> file <span class="keyword">in</span> files]</span><br><span class="line">    docs = []</span><br><span class="line">    <span class="keyword">for</span> loader <span class="keyword">in</span> loaders:</span><br><span class="line">        <span class="keyword">if</span> loader <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">            docs.extend(loader.load())</span><br><span class="line">    <span class="comment"># 切分文档</span></span><br><span class="line">    text_splitter = RecursiveCharacterTextSplitter(</span><br><span class="line">        chunk_size=<span class="number">1500</span>, chunk_overlap=<span class="number">150</span>)</span><br><span class="line">    split_docs = text_splitter.split_documents(docs[:<span class="number">1000</span>])</span><br><span class="line">    <span class="string">&quot;&quot;&quot;小技巧&quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 在每段分割的文档前后加上文档名，这样可以保证查询的信息是指定文档内的</span></span><br><span class="line">    <span class="keyword">for</span> one_chunk <span class="keyword">in</span> split_docs:</span><br><span class="line">        one_chunk.page_content = one_chunk.metadata[<span class="string">&quot;source&quot;</span>].split(<span class="string">&quot;/&quot;</span>)[-<span class="number">1</span>] + one_chunk.page_content + \</span><br><span class="line">                                 one_chunk.metadata[<span class="string">&quot;source&quot;</span>].split(<span class="string">&quot;/&quot;</span>)[-<span class="number">1</span>]</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">type</span>(embeddings) == <span class="built_in">str</span>:</span><br><span class="line">        embeddings = get_embedding(embedding=embeddings)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载数据库</span></span><br><span class="line">    vectordb = Chroma.from_documents(</span><br><span class="line">        documents=split_docs,</span><br><span class="line">        embedding=embeddings,</span><br><span class="line">        persist_directory=persist_directory  <span class="comment"># 允许我们将persist_directory目录保存到磁盘上</span></span><br><span class="line">    )</span><br><span class="line">    vectordb.persist()</span><br><span class="line">    <span class="keyword">return</span> vectordb</span><br></pre></td></tr></table></figure>
<h2 id="大模型API调用"><a href="#大模型API调用" class="headerlink" title="大模型API调用"></a>大模型API调用</h2><h3 id="抖音豆包大模型（新增）"><a href="#抖音豆包大模型（新增）" class="headerlink" title="抖音豆包大模型（新增）"></a>抖音豆包大模型（新增）</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">doubao_llm</span>(<span class="params">prompt: <span class="built_in">str</span>, model: <span class="built_in">str</span>, api_key: <span class="built_in">str</span>, temperature: <span class="built_in">float</span></span>):</span><br><span class="line">    client = Ark(base_url=<span class="string">&quot;https://ark.cn-beijing.volces.com/api/v3&quot;</span>,</span><br><span class="line">                 api_key=api_key,</span><br><span class="line">                 max_retries=<span class="number">5</span>,</span><br><span class="line">                 timeout=<span class="number">600</span>)</span><br><span class="line">    <span class="comment"># Non-streaming:</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;----- standard request -----&quot;</span>)</span><br><span class="line">    completion = client.chat.completions.create(</span><br><span class="line">        model=model,</span><br><span class="line">        messages=[</span><br><span class="line">            &#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;system&quot;</span>, <span class="string">&quot;content&quot;</span>: <span class="string">&quot;系统提示词&quot;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;user&quot;</span>, <span class="string">&quot;content&quot;</span>: prompt&#125;,</span><br><span class="line">        ],</span><br><span class="line">        temperature=temperature</span><br><span class="line">    )</span><br><span class="line">    <span class="keyword">return</span> completion.usage.total_tokens, completion.choices[<span class="number">0</span>].message.content</span><br></pre></td></tr></table></figure>
<h3 id="腾讯的混元大模型"><a href="#腾讯的混元大模型" class="headerlink" title="腾讯的混元大模型"></a>腾讯的混元大模型</h3><p>:warning:混元大模型需要企业认证才可以使用。官方API文档<a target="_blank" rel="noopener external nofollow noreferrer" href="https://cloud.tencent.com/document/api/1729/101836">腾讯混元大模型 腾讯混元大模型标准版-腾讯混元大模型相关接口-API 中心-腾讯云 (tencent.com)</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个函数get_completion_hunyuan，用于调用hunyuan原生接口</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_completion_hunyuan</span>(<span class="params">prompt: <span class="built_in">str</span>, model: <span class="built_in">str</span>, temperature: <span class="built_in">float</span>, secret_id: <span class="built_in">str</span>, secret_key: <span class="built_in">str</span></span>):</span><br><span class="line">    <span class="comment"># 判断是否传入secret_id和secret_key，如果没有传入，则从配置文件中解析</span></span><br><span class="line">    <span class="keyword">if</span> secret_id == <span class="literal">None</span> <span class="keyword">or</span> secret_key == <span class="literal">None</span>:</span><br><span class="line">        secret_id, secret_key = parse_llm_api_key(<span class="string">&quot;hunyuan&quot;</span>)</span><br><span class="line">    <span class="comment"># 使用secret_id和secret_key创建一个认证对象</span></span><br><span class="line">    cred = credential.Credential(secret_id, secret_key)</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 创建一个客户端配置对象，可以设置连接池大小等参数</span></span><br><span class="line">    cpf = ClientProfile()</span><br><span class="line">    <span class="comment"># 设置预先建立连接的数量，可以降低访问延迟</span></span><br><span class="line">    cpf.httpProfile.pre_conn_pool_size = <span class="number">3</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 使用认证对象和客户端配置对象创建一个hunyuan客户端</span></span><br><span class="line">    client = hunyuan_client.HunyuanClient(cred, <span class="string">&quot;ap-guangzhou&quot;</span>, cpf)</span><br><span class="line">    <span class="comment"># 创建一个请求对象</span></span><br><span class="line">    req = models.ChatStdRequest()</span><br><span class="line">    <span class="comment"># 创建一个消息对象，设置角色为用户，内容为传入的prompt</span></span><br><span class="line">    msg = models.Message()</span><br><span class="line">    msg.Role = <span class="string">&quot;user&quot;</span></span><br><span class="line">    msg.Content = prompt</span><br><span class="line">    <span class="comment"># 将消息对象添加到请求对象的Messages列表中</span></span><br><span class="line">    req.Messages = [msg]</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 调用hunyuan客户端的ChatStd方法，传入请求对象，获取响应</span></span><br><span class="line">    resp = client.ChatStd(req)</span><br><span class="line">    <span class="comment"># 打印响应</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;resp:&quot;</span>, resp)</span><br><span class="line">    <span class="comment"># 初始化一个字符串，用于存储完整的响应内容</span></span><br><span class="line">    full_content = <span class="string">&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 遍历响应中的事件</span></span><br><span class="line">    <span class="keyword">for</span> event <span class="keyword">in</span> resp:</span><br><span class="line">        <span class="comment"># 打印事件</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;event&quot;</span>, event)</span><br><span class="line">        <span class="comment"># 将事件中的数据解析为json对象</span></span><br><span class="line">        data = json.loads(event[<span class="string">&#x27;data&#x27;</span>])</span><br><span class="line">        <span class="comment"># 遍历json对象中的Choices列表</span></span><br><span class="line">        <span class="keyword">for</span> choice <span class="keyword">in</span> data[<span class="string">&#x27;Choices&#x27;</span>]:</span><br><span class="line">            <span class="comment"># 将每个Choice中的Content添加到full_content中</span></span><br><span class="line">            full_content += choice[<span class="string">&#x27;Delta&#x27;</span>][<span class="string">&#x27;Content&#x27;</span>]</span><br><span class="line">    <span class="comment"># 打印完整的响应内容</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;full_content:&quot;</span>, full_content)</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 返回完整的响应内容</span></span><br><span class="line">    <span class="keyword">return</span> full_content</span><br></pre></td></tr></table></figure>
<h3 id="阿里巴巴的通义千问"><a href="#阿里巴巴的通义千问" class="headerlink" title="阿里巴巴的通义千问"></a>阿里巴巴的通义千问</h3><p>官方API文档<a target="_blank" rel="noopener external nofollow noreferrer" href="https://help.aliyun.com/zh/dashscope/create-a-chat-foundation-model?spm=a2c4g.11186623.0.0.8ab5f4001AhvFp">快速入门_模型服务灵积(DashScope)-阿里云帮助中心 (aliyun.com)</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个函数get_completion_qwen，用于调用qwen原生接口</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_completion_qwen</span>(<span class="params">prompt: <span class="built_in">str</span>, model: <span class="built_in">str</span>, temperature: <span class="built_in">float</span>, api_key: <span class="built_in">str</span>, max_tokens: <span class="built_in">int</span></span>):</span><br><span class="line">    <span class="comment"># 判断是否传入api_key，如果没有传入，则从配置文件中解析</span></span><br><span class="line">    <span class="keyword">if</span> api_key == <span class="literal">None</span>:</span><br><span class="line">        api_key = parse_llm_api_key(<span class="string">&quot;qwen&quot;</span>)</span><br><span class="line">    <span class="comment"># 设置dashscope的api_key，dashscope是一个第三方库，用于简化API调用</span></span><br><span class="line">    dashscope.api_key = api_key</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 创建一个消息列表，包含用户的消息</span></span><br><span class="line">    messages = [&#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;user&quot;</span>, <span class="string">&quot;content&quot;</span>: prompt&#125;]</span><br><span class="line">    <span class="comment"># 调用dashscope.Generation.call方法，传入模型名称、消息列表、温度系数、最大回复长度和结果格式</span></span><br><span class="line">    response = dashscope.Generation.call(</span><br><span class="line">        model=model,  <span class="comment"># 模型名称</span></span><br><span class="line">        messages=messages,  <span class="comment"># 消息列表</span></span><br><span class="line">        temperature=temperature,  <span class="comment"># 模型输出的温度系数，控制输出的随机程度</span></span><br><span class="line">        max_tokens=max_tokens,  <span class="comment"># 回复最大长度</span></span><br><span class="line">        result_format=<span class="string">&#x27;message&#x27;</span>,  <span class="comment"># 设置结果格式为&quot;message&quot;</span></span><br><span class="line">    )</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 打印响应</span></span><br><span class="line">    <span class="built_in">print</span>(response)</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 从响应中提取回复内容并返回</span></span><br><span class="line">    <span class="keyword">return</span> response.output.choices[<span class="number">0</span>].message[<span class="string">&quot;content&quot;</span>]</span><br></pre></td></tr></table></figure>
<h3 id="OpenAI的ChatGPT"><a href="#OpenAI的ChatGPT" class="headerlink" title="OpenAI的ChatGPT"></a>OpenAI的ChatGPT</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个函数get_completion_gpt，用于调用OpenAI原生接口</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_completion_gpt</span>(<span class="params">prompt: <span class="built_in">str</span>, model: <span class="built_in">str</span>, temperature: <span class="built_in">float</span>, api_key: <span class="built_in">str</span>, max_tokens: <span class="built_in">int</span></span>):</span><br><span class="line">    <span class="comment"># 判断是否传入api_key，如果没有传入，则从配置文件中解析</span></span><br><span class="line">    <span class="keyword">if</span> api_key == <span class="literal">None</span>:</span><br><span class="line">        api_key = parse_llm_api_key(<span class="string">&quot;openai&quot;</span>)</span><br><span class="line">    <span class="comment"># 设置OpenAI的api_key，openai是一个第三方库，用于简化API调用</span></span><br><span class="line">    openai.api_key = api_key</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 创建一个消息列表，包含用户的消息</span></span><br><span class="line">    messages = [&#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;user&quot;</span>, <span class="string">&quot;content&quot;</span>: prompt&#125;]</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 调用openai.ChatCompletion.create方法，传入模型名称、消息列表、温度系数和最大回复长度</span></span><br><span class="line">    response = openai.ChatCompletion.create(</span><br><span class="line">        model=model,  <span class="comment"># 模型名称</span></span><br><span class="line">        messages=messages,  <span class="comment"># 消息列表</span></span><br><span class="line">        temperature=temperature,  <span class="comment"># 模型输出的温度系数，控制输出的随机程度</span></span><br><span class="line">        max_tokens=max_tokens,  <span class="comment"># 回复最大长度</span></span><br><span class="line">    )</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 从响应中提取回复内容并返回</span></span><br><span class="line">    <span class="keyword">return</span> response.choices[<span class="number">0</span>].message[<span class="string">&quot;content&quot;</span>]</span><br></pre></td></tr></table></figure>
<h3 id="百度的文心一言"><a href="#百度的文心一言" class="headerlink" title="百度的文心一言"></a>百度的文心一言</h3><p>官方API文档<a target="_blank" rel="noopener external nofollow noreferrer" href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/flfmc9do2">API介绍 - 千帆大模型平台 | 百度智能云文档 (baidu.com)</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">get_access_token</span>(<span class="params">api_key, secret_key</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    使用 API Key，Secret Key 获取access_token，替换下列示例中的应用API Key、应用Secret Key</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 指定网址</span></span><br><span class="line">    url = <span class="string">f&quot;https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&amp;client_id=<span class="subst">&#123;api_key&#125;</span>&amp;client_secret=<span class="subst">&#123;secret_key&#125;</span>&quot;</span></span><br><span class="line">    <span class="comment"># 设置 POST 访问</span></span><br><span class="line">    payload = json.dumps(<span class="string">&quot;&quot;</span>)</span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;Accept&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment"># 通过 POST 访问获取账户对应的 access_token</span></span><br><span class="line">    response = requests.request(<span class="string">&quot;POST&quot;</span>, url, headers=headers, data=payload)</span><br><span class="line">    <span class="keyword">return</span> response.json().get(<span class="string">&quot;access_token&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_completion_wenxin</span>(<span class="params">prompt: <span class="built_in">str</span>, model: <span class="built_in">str</span>, temperature: <span class="built_in">float</span>, api_key: <span class="built_in">str</span>, secret_key: <span class="built_in">str</span></span>):</span><br><span class="line">    <span class="comment"># 封装百度文心原生接口</span></span><br><span class="line">    <span class="keyword">if</span> api_key == <span class="literal">None</span> <span class="keyword">or</span> secret_key == <span class="literal">None</span>:</span><br><span class="line">        api_key, secret_key = parse_llm_api_key(<span class="string">&quot;wenxin&quot;</span>)</span><br><span class="line">    <span class="comment"># 获取access_token</span></span><br><span class="line">    access_token = get_access_token(api_key, secret_key)</span><br><span class="line">    <span class="comment"># 调用接口</span></span><br><span class="line">    url = <span class="string">f&quot;https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=<span class="subst">&#123;access_token&#125;</span>&quot;</span></span><br><span class="line">    <span class="comment"># 配置 POST 参数</span></span><br><span class="line">    payload = json.dumps(&#123;</span><br><span class="line">        <span class="string">&quot;messages&quot;</span>: [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">&quot;role&quot;</span>: <span class="string">&quot;user&quot;</span>,  <span class="comment"># user prompt</span></span><br><span class="line">                <span class="string">&quot;content&quot;</span>: <span class="string">&quot;&#123;&#125;&quot;</span>.<span class="built_in">format</span>(prompt)  <span class="comment"># 输入的 prompt</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;)</span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment"># 发起请求</span></span><br><span class="line">    response = requests.request(<span class="string">&quot;POST&quot;</span>, url, headers=headers, data=payload)</span><br><span class="line">    <span class="comment"># 返回的是一个 Json 字符串</span></span><br><span class="line">    js = json.loads(response.text)</span><br><span class="line">    <span class="keyword">return</span> js[<span class="string">&quot;result&quot;</span>]</span><br></pre></td></tr></table></figure>
<h3 id="科大讯飞的讯飞星火认知大模型"><a href="#科大讯飞的讯飞星火认知大模型" class="headerlink" title="科大讯飞的讯飞星火认知大模型"></a>科大讯飞的讯飞星火认知大模型</h3><p>官方API文档<a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.xfyun.cn/doc/spark/Web.html">星火认知大模型Web API文档 | 讯飞开放平台文档中心 (xfyun.cn)</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">get_completion_spark</span>(<span class="params">prompt: <span class="built_in">str</span>, model: <span class="built_in">str</span>, temperature: <span class="built_in">float</span>, api_key: <span class="built_in">str</span>, appid: <span class="built_in">str</span>, api_secret: <span class="built_in">str</span>,</span></span><br><span class="line"><span class="params">                         max_tokens: <span class="built_in">int</span></span>):</span><br><span class="line">    <span class="keyword">if</span> api_key == <span class="literal">None</span> <span class="keyword">or</span> appid == <span class="literal">None</span> <span class="keyword">and</span> api_secret == <span class="literal">None</span>:</span><br><span class="line">        api_key, appid, api_secret = parse_llm_api_key(<span class="string">&quot;spark&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 配置 1.5、2、3、3.5 的不同环境</span></span><br><span class="line">    <span class="keyword">if</span> model == <span class="string">&quot;Spark-1.5&quot;</span>:</span><br><span class="line">        domain = <span class="string">&quot;general&quot;</span></span><br><span class="line">        Spark_url = <span class="string">&quot;wss://spark-api.xf-yun.com/v1.1/chat&quot;</span>  <span class="comment"># v1.5环境的地址</span></span><br><span class="line">    <span class="keyword">if</span> model == <span class="string">&quot;Spark-2.0&quot;</span>:</span><br><span class="line">        domain = <span class="string">&quot;generalv2&quot;</span>  <span class="comment"># v2.0版本</span></span><br><span class="line">        Spark_url = <span class="string">&quot;wss://spark-api.xf-yun.com/v2.1/chat&quot;</span>  <span class="comment"># v2.0环境的地址</span></span><br><span class="line">    <span class="keyword">if</span> model == <span class="string">&quot;Spark-3.0&quot;</span>:</span><br><span class="line">        domain = <span class="string">&quot;generalv3&quot;</span>  <span class="comment"># v3.0版本</span></span><br><span class="line">        Spark_url = <span class="string">&quot;wss://spark-api.xf-yun.com/v3.1/chat&quot;</span>  <span class="comment"># v3.0环境的地址</span></span><br><span class="line">    <span class="keyword">if</span> model == <span class="string">&quot;Spark-3.5&quot;</span>:</span><br><span class="line">        domain = <span class="string">&quot;generalv3.5&quot;</span>  <span class="comment"># v3.5版本</span></span><br><span class="line">        Spark_url = <span class="string">&quot;wss://spark-api.xf-yun.com/v3.5/chat&quot;</span>  <span class="comment"># v3.5环境的地址</span></span><br><span class="line"></span><br><span class="line">    question = [&#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;user&quot;</span>, <span class="string">&quot;content&quot;</span>: prompt&#125;]</span><br><span class="line">    response = spark_main(appid, api_key, api_secret, Spark_url, domain, question, temperature, max_tokens)</span><br><span class="line">    <span class="keyword">return</span> response</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Ws_Param</span>(<span class="title class_ inherited__">object</span>):</span><br><span class="line">    <span class="comment"># 初始化</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, APPID, APIKey, APISecret, Spark_url</span>):</span><br><span class="line">        self.APPID = APPID</span><br><span class="line">        self.APIKey = APIKey</span><br><span class="line">        self.APISecret = APISecret</span><br><span class="line">        self.host = urlparse(Spark_url).netloc</span><br><span class="line">        self.path = urlparse(Spark_url).path</span><br><span class="line">        self.Spark_url = Spark_url</span><br><span class="line">        <span class="comment"># 自定义</span></span><br><span class="line">        self.temperature = <span class="number">0</span></span><br><span class="line">        self.max_tokens = <span class="number">2048</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 生成url</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">create_url</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="comment"># 生成RFC1123格式的时间戳</span></span><br><span class="line">        now = datetime.now()</span><br><span class="line">        date = format_date_time(mktime(now.timetuple()))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 拼接字符串</span></span><br><span class="line">        signature_origin = <span class="string">&quot;host: &quot;</span> + self.host + <span class="string">&quot;\n&quot;</span></span><br><span class="line">        signature_origin += <span class="string">&quot;date: &quot;</span> + date + <span class="string">&quot;\n&quot;</span></span><br><span class="line">        signature_origin += <span class="string">&quot;GET &quot;</span> + self.path + <span class="string">&quot; HTTP/1.1&quot;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 进行hmac-sha256进行加密</span></span><br><span class="line">        signature_sha = hmac.new(self.APISecret.encode(<span class="string">&#x27;utf-8&#x27;</span>), signature_origin.encode(<span class="string">&#x27;utf-8&#x27;</span>),</span><br><span class="line">                                 digestmod=hashlib.sha256).digest()</span><br><span class="line"></span><br><span class="line">        signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line">        authorization_origin = <span class="string">f&#x27;api_key=&quot;<span class="subst">&#123;self.APIKey&#125;</span>&quot;, algorithm=&quot;hmac-sha256&quot;, headers=&quot;host date request-line&quot;, signature=&quot;<span class="subst">&#123;signature_sha_base64&#125;</span>&quot;&#x27;</span></span><br><span class="line"></span><br><span class="line">        authorization = base64.b64encode(authorization_origin.encode(<span class="string">&#x27;utf-8&#x27;</span>)).decode(encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 将请求的鉴权参数组合为字典</span></span><br><span class="line">        v = &#123;</span><br><span class="line">            <span class="string">&quot;authorization&quot;</span>: authorization,</span><br><span class="line">            <span class="string">&quot;date&quot;</span>: date,</span><br><span class="line">            <span class="string">&quot;host&quot;</span>: self.host</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment"># 拼接鉴权参数，生成url</span></span><br><span class="line">        url = self.Spark_url + <span class="string">&#x27;?&#x27;</span> + urlencode(v)</span><br><span class="line">        <span class="comment"># 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释，比对相同参数时生成的url与自己代码生成的url是否一致</span></span><br><span class="line">        <span class="keyword">return</span> url</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 收到websocket错误的处理</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">on_error</span>(<span class="params">ws, error</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;### error:&quot;</span>, error)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 收到websocket关闭的处理</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">on_close</span>(<span class="params">ws, one, two</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot; &quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 收到websocket连接建立的处理</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">on_open</span>(<span class="params">ws</span>):</span><br><span class="line">    thread.start_new_thread(run, (ws,))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">ws, *args</span>):</span><br><span class="line">    data = json.dumps(gen_params(appid=ws.appid, domain=ws.domain, question=ws.question, temperature=ws.temperature,</span><br><span class="line">                                 max_tokens=ws.max_tokens))</span><br><span class="line">    ws.send(data)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 收到websocket消息的处理</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">on_message</span>(<span class="params">ws, message</span>):</span><br><span class="line">    <span class="comment"># print(message)</span></span><br><span class="line">    data = json.loads(message)</span><br><span class="line">    code = data[<span class="string">&#x27;header&#x27;</span>][<span class="string">&#x27;code&#x27;</span>]</span><br><span class="line">    <span class="keyword">if</span> code != <span class="number">0</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;请求错误: <span class="subst">&#123;code&#125;</span>, <span class="subst">&#123;data&#125;</span>&#x27;</span>)</span><br><span class="line">        ws.close()</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        choices = data[<span class="string">&quot;payload&quot;</span>][<span class="string">&quot;choices&quot;</span>]</span><br><span class="line">        status = choices[<span class="string">&quot;status&quot;</span>]</span><br><span class="line">        content = choices[<span class="string">&quot;text&quot;</span>][<span class="number">0</span>][<span class="string">&quot;content&quot;</span>]</span><br><span class="line">        <span class="built_in">print</span>(content, end=<span class="string">&quot;&quot;</span>)</span><br><span class="line">        <span class="keyword">global</span> answer</span><br><span class="line">        answer += content</span><br><span class="line">        <span class="comment"># print(1)</span></span><br><span class="line">        <span class="keyword">if</span> status == <span class="number">2</span>:</span><br><span class="line">            ws.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">gen_params</span>(<span class="params">appid, domain, question, temperature, max_tokens</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    通过appid和用户的提问来生成请参数</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">&quot;header&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;app_id&quot;</span>: appid,</span><br><span class="line">            <span class="string">&quot;uid&quot;</span>: <span class="string">&quot;1234&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;parameter&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;chat&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;domain&quot;</span>: domain,</span><br><span class="line">                <span class="string">&quot;random_threshold&quot;</span>: <span class="number">0.5</span>,</span><br><span class="line">                <span class="string">&quot;max_tokens&quot;</span>: max_tokens,</span><br><span class="line">                <span class="string">&quot;temperature&quot;</span>: temperature,</span><br><span class="line">                <span class="string">&quot;auditing&quot;</span>: <span class="string">&quot;default&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;payload&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;message&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;text&quot;</span>: question</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> data</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">spark_main</span>(<span class="params">appid, api_key, api_secret, Spark_url, domain, question, temperature, max_tokens</span>):</span><br><span class="line">    <span class="comment"># print(&quot;星火:&quot;)</span></span><br><span class="line">    output_queue = queue.Queue()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">on_message</span>(<span class="params">ws, message</span>):</span><br><span class="line">        data = json.loads(message)</span><br><span class="line">        code = data[<span class="string">&#x27;header&#x27;</span>][<span class="string">&#x27;code&#x27;</span>]</span><br><span class="line">        <span class="keyword">if</span> code != <span class="number">0</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&#x27;请求错误: <span class="subst">&#123;code&#125;</span>, <span class="subst">&#123;data&#125;</span>&#x27;</span>)</span><br><span class="line">            ws.close()</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            choices = data[<span class="string">&quot;payload&quot;</span>][<span class="string">&quot;choices&quot;</span>]</span><br><span class="line">            status = choices[<span class="string">&quot;status&quot;</span>]</span><br><span class="line">            content = choices[<span class="string">&quot;text&quot;</span>][<span class="number">0</span>][<span class="string">&quot;content&quot;</span>]</span><br><span class="line">            <span class="comment"># print(content, end=&#x27;&#x27;)</span></span><br><span class="line">            <span class="comment"># 将输出值放入队列</span></span><br><span class="line">            output_queue.put(content)</span><br><span class="line">            <span class="keyword">if</span> status == <span class="number">2</span>:</span><br><span class="line">                ws.close()</span><br><span class="line"></span><br><span class="line">    wsParam = Ws_Param(appid, api_key, api_secret, Spark_url)</span><br><span class="line">    websocket.enableTrace(<span class="literal">False</span>)</span><br><span class="line">    wsUrl = wsParam.create_url()</span><br><span class="line">    ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)</span><br><span class="line">    ws.appid = appid</span><br><span class="line">    ws.question = question</span><br><span class="line">    ws.domain = domain</span><br><span class="line">    ws.temperature = temperature</span><br><span class="line">    ws.max_tokens = max_tokens</span><br><span class="line">    ws.run_forever(sslopt=&#123;<span class="string">&quot;cert_reqs&quot;</span>: ssl.CERT_NONE&#125;)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span>.join([output_queue.get() <span class="keyword">for</span> _ <span class="keyword">in</span> <span class="built_in">range</span>(output_queue.qsize())])</span><br></pre></td></tr></table></figure>
<h3 id="智谱清言的ChartGLM（2024-08最新调用）"><a href="#智谱清言的ChartGLM（2024-08最新调用）" class="headerlink" title="智谱清言的ChartGLM（2024.08最新调用）"></a>智谱清言的ChartGLM（2024.08最新调用）</h3><p>官方API文档<a target="_blank" rel="noopener external nofollow noreferrer" href="https://open.bigmodel.cn/dev/api">智谱AI开放平台 (bigmodel.cn)</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> zhipuai <span class="keyword">import</span> ZhipuAI</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_completion_glm</span>(<span class="params">prompt: <span class="built_in">str</span>, model: <span class="built_in">str</span>, temperature: <span class="built_in">float</span>, api_key: <span class="built_in">str</span>, max_tokens: <span class="built_in">int</span></span>):</span><br><span class="line">    <span class="comment"># 判断是否传入api_key，如果没有传入，则从配置文件中解析</span></span><br><span class="line">    <span class="keyword">if</span> api_key <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">        api_key = parse_llm_api_key(<span class="string">&quot;zhipuai&quot;</span>)</span><br><span class="line">    <span class="comment"># 设置zhipuai的api_key，zhipuai是一个第三方库，用于简化API调用</span></span><br><span class="line">    client = ZhipuAI(api_key=api_key)  <span class="comment"># 填写您自己的APIKey</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 调用 client.chat.completions.create方法，传入模型名称、提示、温度系数和最大回复长度</span></span><br><span class="line">    response = client.chat.completions.create(</span><br><span class="line">        model=model,  <span class="comment"># 模型名称</span></span><br><span class="line">        messages=[</span><br><span class="line">            &#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;user&quot;</span>, <span class="string">&quot;content&quot;</span>: prompt&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;system&quot;</span>, <span class="string">&quot;content&quot;</span>: <span class="string">&quot;系统提示词&quot;</span>&#125;,</span><br><span class="line">        ],  <span class="comment"># 提示</span></span><br><span class="line">        temperature=temperature,  <span class="comment"># 温度系数</span></span><br><span class="line">        max_tokens=max_tokens  <span class="comment"># 最大回复长度</span></span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 从响应中提取回复内容，并去除首尾的引号和空格</span></span><br><span class="line">    <span class="keyword">return</span> response.choices[<span class="number">0</span>].message.content</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="获取本地-env配置文件的api-key"><a href="#获取本地-env配置文件的api-key" class="headerlink" title="获取本地.env配置文件的api_key"></a>获取本地.env配置文件的api_key</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> dotenv <span class="keyword">import</span> load_dotenv, find_dotenv</span><br><span class="line"><span class="keyword">from</span> langchain.utils <span class="keyword">import</span> get_from_dict_or_env</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">parse_llm_api_key</span>(<span class="params">model: <span class="built_in">str</span>, env_file: <span class="built_in">dict</span>(<span class="params"></span>) = <span class="literal">None</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    通过 model 和 env_file 的来解析平台参数</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">if</span> env_file <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">        _ = load_dotenv(find_dotenv())</span><br><span class="line">        env_file = os.environ</span><br><span class="line">    <span class="keyword">if</span> model == <span class="string">&quot;openai&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> env_file[<span class="string">&quot;OPENAI_API_KEY&quot;</span>]</span><br><span class="line">    <span class="keyword">elif</span> model == <span class="string">&quot;wenxin&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> env_file[<span class="string">&quot;wenxin_api_key&quot;</span>], env_file[<span class="string">&quot;wenxin_secret_key&quot;</span>]</span><br><span class="line">    <span class="keyword">elif</span> model == <span class="string">&quot;spark&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> env_file[<span class="string">&quot;spark_api_key&quot;</span>], env_file[<span class="string">&quot;spark_appid&quot;</span>], env_file[<span class="string">&quot;spark_api_secret&quot;</span>]</span><br><span class="line">    <span class="keyword">elif</span> model == <span class="string">&quot;zhipuai&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> get_from_dict_or_env(env_file, <span class="string">&quot;zhipuai_api_key&quot;</span>, <span class="string">&quot;ZHIPUAI_API_KEY&quot;</span>)</span><br><span class="line">        <span class="comment"># return env_file[&quot;ZHIPUAI_API_KEY&quot;]</span></span><br><span class="line">    <span class="keyword">elif</span> model == <span class="string">&quot;qwen&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> env_file[<span class="string">&quot;QWEN_API_KEY&quot;</span>]</span><br><span class="line">    <span class="keyword">elif</span> model == <span class="string">&quot;hunyuan&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> env_file[<span class="string">&quot;hunyuan_secret_id&quot;</span>], env_file[<span class="string">&quot;hunyuan_secret_key&quot;</span>]</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">raise</span> ValueError(<span class="string">f&quot;model<span class="subst">&#123;model&#125;</span> not support!!!&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://huaiyuechusan.github.io">SanShui</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://huaiyuechusan.github.io/archives/763f7a6d.html">https://huaiyuechusan.github.io/archives/763f7a6d.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://huaiyuechusan.github.io" target="_blank">SanShui的个人博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%A4%A7%E6%A8%A1%E5%9E%8B/">大模型</a><a class="post-meta__tags" href="/tags/Python/">Python</a></div><div class="post_share"><div class="social-share" data-image="http://wallpaper.csun.site/?12" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/archives/9ac21f23.html"><img class="prev-cover" src="http://wallpaper.csun.site/?9" onerror="onerror=null;src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">本地基于知识库的大模型的使用教程</div></div></a></div><div class="next-post pull-right"><a href="/archives/c3b7887e.html"><img class="next-cover" src="http://wallpaper.csun.site/?10" onerror="onerror=null;src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">从零入门CV图像竞赛（2024全球Deepfake攻防挑战赛）</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/archives/9ac21f23.html" title="本地基于知识库的大模型的使用教程"><img class="cover" src="http://wallpaper.csun.site/?9" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">本地基于知识库的大模型的使用教程</div></div></a></div><div><a href="/archives/984515cf.html" title="Python 网络与并发编程"><img class="cover" src="http://wallpaper.csun.site/?6" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">Python 网络与并发编程</div></div></a></div><div><a href="/archives/fab44cdf.html" title="函数式编程和Python新核心特性"><img class="cover" src="http://wallpaper.csun.site/?7" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">函数式编程和Python新核心特性</div></div></a></div><div><a href="/archives/8df5f8b9.html" title="Python深入和提高"><img class="cover" src="http://wallpaper.csun.site/?5" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">Python深入和提高</div></div></a></div><div><a href="/archives/d9ef328.html" title="Python入门"><img class="cover" src="http://wallpaper.csun.site/?4" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">Python入门</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81OTEzMS8zNTU5Mw=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/./img/config_img/%E9%98%B3%E5%85%89%E5%B0%8F%E7%8C%AB.jpg" onerror="this.onerror=null;this.src='/./img/config_img/%E8%93%9D%E5%A4%A9.jpg'" alt="avatar"/></div><div class="author-info__name">SanShui</div><div class="author-info__description">今天不学习，明天变垃圾</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">25</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">16</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><a id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/huaiyuechusan"><i class="fa-sharp fa-solid fa-plane"></i><span>欢迎关注我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/huaiyuechusan" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://huaiyuechusan.github.io/atom.xml" target="_blank" title=""><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到SanShui的博客</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%BC%80%E5%8F%91%E5%AE%9E%E6%88%98"><span class="toc-text">大模型开发实战</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%90%91%E9%87%8F%E6%95%B0%E6%8D%AE%E5%BA%93%E5%88%9B%E5%BB%BA"><span class="toc-text">大模型向量数据库创建</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E4%B8%8A%E4%BC%A0%E7%9A%84%E4%B8%8D%E5%90%8C%E6%A0%BC%E5%BC%8F%E6%96%87%E4%BB%B6%E8%BF%9B%E8%A1%8C%E5%A4%84%E7%90%86"><span class="toc-text">对上传的不同格式文件进行处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%87%E5%88%86%E6%96%87%E6%A1%A3"><span class="toc-text">切分文档</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%A7%E6%A8%A1%E5%9E%8BAPI%E8%B0%83%E7%94%A8"><span class="toc-text">大模型API调用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8A%96%E9%9F%B3%E8%B1%86%E5%8C%85%E5%A4%A7%E6%A8%A1%E5%9E%8B%EF%BC%88%E6%96%B0%E5%A2%9E%EF%BC%89"><span class="toc-text">抖音豆包大模型（新增）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%85%BE%E8%AE%AF%E7%9A%84%E6%B7%B7%E5%85%83%E5%A4%A7%E6%A8%A1%E5%9E%8B"><span class="toc-text">腾讯的混元大模型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4%E7%9A%84%E9%80%9A%E4%B9%89%E5%8D%83%E9%97%AE"><span class="toc-text">阿里巴巴的通义千问</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#OpenAI%E7%9A%84ChatGPT"><span class="toc-text">OpenAI的ChatGPT</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%99%BE%E5%BA%A6%E7%9A%84%E6%96%87%E5%BF%83%E4%B8%80%E8%A8%80"><span class="toc-text">百度的文心一言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A7%91%E5%A4%A7%E8%AE%AF%E9%A3%9E%E7%9A%84%E8%AE%AF%E9%A3%9E%E6%98%9F%E7%81%AB%E8%AE%A4%E7%9F%A5%E5%A4%A7%E6%A8%A1%E5%9E%8B"><span class="toc-text">科大讯飞的讯飞星火认知大模型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%99%BA%E8%B0%B1%E6%B8%85%E8%A8%80%E7%9A%84ChartGLM%EF%BC%882024-08%E6%9C%80%E6%96%B0%E8%B0%83%E7%94%A8%EF%BC%89"><span class="toc-text">智谱清言的ChartGLM（2024.08最新调用）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%9C%AC%E5%9C%B0-env%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%9A%84api-key"><span class="toc-text">获取本地.env配置文件的api_key</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/archives/6ca065c5.html" title="SanShui API 使用教程"><img src="http://wallpaper.csun.site/?abc" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="SanShui API 使用教程"/></a><div class="content"><a class="title" href="/archives/6ca065c5.html" title="SanShui API 使用教程">SanShui API 使用教程</a><time datetime="2025-03-13T13:21:12.311Z" title="更新于 2025-03-13 21:21:12">2025-03-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/b5700173.html" title="微信小程序定时订阅消息问题"><img src="http://wallpaper.csun.site/?25" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="微信小程序定时订阅消息问题"/></a><div class="content"><a class="title" href="/archives/b5700173.html" title="微信小程序定时订阅消息问题">微信小程序定时订阅消息问题</a><time datetime="2024-10-17T12:04:53.379Z" title="更新于 2024-10-17 20:04:53">2024-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/638889b7.html" title="深度学习回归任务训练代码模版"><img src="http://wallpaper.csun.site/?24" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="深度学习回归任务训练代码模版"/></a><div class="content"><a class="title" href="/archives/638889b7.html" title="深度学习回归任务训练代码模版">深度学习回归任务训练代码模版</a><time datetime="2024-10-17T12:04:09.066Z" title="更新于 2024-10-17 20:04:09">2024-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/7cb9f0a8.html" title="通过nginx访问tomcat中SpringMVC应用"><img src="http://wallpaper.csun.site/?23" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="通过nginx访问tomcat中SpringMVC应用"/></a><div class="content"><a class="title" href="/archives/7cb9f0a8.html" title="通过nginx访问tomcat中SpringMVC应用">通过nginx访问tomcat中SpringMVC应用</a><time datetime="2024-10-17T12:02:23.321Z" title="更新于 2024-10-17 20:02:23">2024-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/d4b1abad.html" title="下载M3U8格式加密视频"><img src="http://wallpaper.csun.site/?22" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="下载M3U8格式加密视频"/></a><div class="content"><a class="title" href="/archives/d4b1abad.html" title="下载M3U8格式加密视频">下载M3U8格式加密视频</a><time datetime="2024-10-17T12:02:02.720Z" title="更新于 2024-10-17 20:02:02">2024-10-17</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('http://wallpaper.csun.site/?12')"><div id="footer-wrap"><div class="footer_custom_text">更多内容查看<a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/huaiyuechusan/">我的GitHub</a> ！</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight, 500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !true) {
  if (true) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><div class="aplayer no-destroy" data-id="875151895" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true" data-volume="0.5"> </div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="false" data-text="Nice,To,Meet,YOU" data-fontsize="15px" data-random="false" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["meta[property=\"og:image\"]","meta[property=\"og:title\"]","meta[property=\"og:url\"]","head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>
    function butterfly_categories_card_injector_config(){
      var parent_div_git = document.getElementById('recent-posts');
      var item_html = '<style>li.categoryBar-list-item{width:32.3%;}.categoryBar-list{max-height: 380px;overflow:auto;}.categoryBar-list::-webkit-scrollbar{width:0!important}@media screen and (max-width: 650px){.categoryBar-list{max-height: 320px;}}</style><div class="recent-post-item" style="height:auto;width:100%;padding:0px;"><div id="categoryBar"><ul class="categoryBar-list"><li class="categoryBar-list-item" style="background:url(./img/config_img/image-20231112202739060-2023-11-1220-27-56.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/AI大模型/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">AI大模型</a><span class="categoryBar-list-count">3</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/202310300007012-2023-11-1123-30-55.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/微信小程序/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">微信小程序</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/Girl-2023-11-2221_46_26.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/爬虫/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">爬虫</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/202310300007316-2023-11-1123-30-57.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/深度学习/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">深度学习</a><span class="categoryBar-list-count">4</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/Cat-2023-11-2221-47-16.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/大数据/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">大数据</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-36-46.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/PyTorch/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">PyTorch</a><span class="categoryBar-list-count">3</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-36-11.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/多模态推荐系统/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">多模态推荐系统</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/Starry-Night-in-Anime-Wallpaper-2023-11-2221-46-39.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/学习总结/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">学习总结</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/page_img/wallhaven-y8lqo7.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/竞赛/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">竞赛</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-34-25.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/Python/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">Python</a><span class="categoryBar-list-count">4</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-37-57.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/Liunx/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">Liunx</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li></ul></div></div>';
      console.log('已挂载butterfly_categories_card')
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
      }
    if( document.getElementById('recent-posts') && (location.pathname ==='/'|| '/' ==='all')){
    butterfly_categories_card_injector_config()
    }
  </script><script data-pjax>
  function butterfly_footer_beautify_injector_config(){
    var parent_div_git = document.getElementById('footer-wrap');
    var item_html = '<div id="workboard"></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="博客框架为Hexo_v6.2.0" title=""><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&amp;logo=hexo" alt=""/></a><a class="github-badge" target="_blank" href="https://butterfly.js.org/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="主题版本Butterfly_v4.3.1" title=""><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&amp;logo=bitdefender" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站采用多线部署，主线路托管于Github Pages" title=""><img src="https://img.shields.io/badge/Hosted-Github Pages-brightgreen?style=flat&amp;logo=Github" alt=""/></a><a class="github-badge" target="_blank" href="https://gitee.com/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站采用多线部署，备用线路托管于Gitee Pages" title=""><img src="https://img.shields.io/badge/Hosted-Gitee Pages-22DDDD?style=flat&amp;logo=Gitee" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站项目由Github托管" title=""><img src="https://img.shields.io/badge/Source-Github-d021d6?style=flat&amp;logo=GitHub" alt=""/></a><a class="github-badge" target="_blank" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可" title=""><img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&amp;logo=Claris" alt=""/></a></p>';
    console.log('已挂载butterfly_footer_beautify')
    parent_div_git.insertAdjacentHTML("beforeend",item_html)
    }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_footer_beautify_injector_config();
  }
  else if (epage === cpage){
    butterfly_footer_beautify_injector_config();
  }
  </script><script async src="/./js/runtime.js"></script><!-- hexo injector body_end end --></body></html>